<?php

//for this to work db table image_name raw has to be change and gallery.php as well
require 'config.php';

$status = $status_msg = $temp = "";

if(isset($_POST["Submit"])){
	$name = $_FILES["imageUpload"]["name"];

	//Select the file type
	$filename = basename($_FILES["imageUpload"]["name"]);
	$filetype = strtolower(pathinfo($filename, PATHINFO_EXTENSION));

	//Allow certain type of images

	$allowTypes = array('jpg','jpeg','png', 'gif');
	if(in_array($filetype, $allowTypes)){
		
		//Convert to base_64
		$image_base64 = base64_encode(file_get_contents($_FILES["imageUpload"]["tmp_name"]));
		$image = 'data:image/'.$filetype.';base64,'.$image_base64;

		//Insert the image into DB
		$sql = "INSERT INTO images(image) VALUES('".$image."');";

		if($conn->query($sql) == TRUE){
			$status = 'Success';
			$status_msg = 'Image Uploaded successfully.';
		}
		else
		{
			$status = 'Failed';
			$status_msg = 'Image Upload Failed! Please Try again.';
		}
	}
	else
	{
		$status_msg = "Please select an image of the following types: JPG, JPEG, PNG, GIF.";
	}

}

echo $status_msg;

echo '<br><a href="gallery.php">View Image Gallery</a>';


?>